package com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.common.util.BusinessServiceUtil;
import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.HealthCareService;
import com.paic.mhis.hcpms.agreement.healthcareprovider.dto.HealthCareProviderDTO;
import com.paic.mhis.hcpms.agreement.healthcareprovider.dto.HealthCareProviderListDTO;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HealthCareConstants;
import com.paic.pafa.app.biz.service.BaseService;
import com.paic.pafa.app.biz.service.BusinessServiceException;

@Component("healthCareService")
public class HealthCareServiceImpl extends BaseService implements
		HealthCareService, BeanFactoryAware {

	@Autowired
	private CommonDAO commonDAO;

	// @Autowired
	// private IMService imService;

	@Override
	public Map<String, Object> addProvider(JSONObject transferJson)
			throws BusinessServiceException {
		HealthCareProviderDTO hcpDTO = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			hcpDTO = JSONObject.toJavaObject(transferJson,
					HealthCareProviderDTO.class);
			String id = commonDAO.getUUID();
			hcpDTO.setProviderid(id);
			commonDAO.insert("healthcare.addProvider", hcpDTO);
			resultMap.put(Constants.SUCCESS_RESULT_KEY, id);
			resultMap.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			resultMap.put(Constants.MSG_KEY, Constants.INSERT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
			e.printStackTrace();
		}
		return resultMap;
	}

	// public void setImService(IMService imService) {
	// this.imService = imService;
	// }

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> queryProvider(JSONObject transferJson)
			throws BusinessServiceException {
		HealthCareProviderDTO hcpDTO = null;
		Map<String, Object> result = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List imgList = null;
		try {
			hcpDTO = JSONObject.toJavaObject(transferJson,
					HealthCareProviderDTO.class);
			result = (Map<String, Object>) commonDAO.get(
					"healthcare.queryProvider", hcpDTO);
			String businessno = "";
			if (result != null && result.size() != 0) {
				businessno = (String) result.get("providerid");
				imgList = commonDAO
						.query("healthcare.queryImgList", businessno);
				result.put("imgList", imgList);

			}
			resultMap.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			resultMap.put(Constants.MSG_KEY, Constants.SELECT_SUCCESS);
			resultMap.put(Constants.SUCCESS_RESULT_KEY, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> queryProviderList(JSONObject transferJson)
			throws BusinessServiceException {
		HealthCareProviderListDTO hcpDTO = new HealthCareProviderListDTO();
		Map<String, Object> result = null;
		try {
			hcpDTO.setQueryParams(JSONObject.toJavaObject(transferJson,
					Map.class));
			int page = Integer.parseInt(transferJson.getString("page"));
			int pageSize = Integer.parseInt(transferJson.getString("pageSize"));
			hcpDTO.setStart(page);
			hcpDTO.setEnd(pageSize);
			hcpDTO.setLimit(pageSize);
			hcpDTO.setDoCount(true);
			hcpDTO.setQueryAll(false);

			result = (Map<String, Object>) commonDAO.queryPage(
					"healthcare.queryProviderList", hcpDTO);
			result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			result.put(Constants.MSG_KEY, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Map<String, Object> updateProvider(JSONObject transferJson)
			throws BusinessServiceException {
		HealthCareProviderDTO hcpDTO = null;
		HealthCareProviderDTO hcpDTOtemp = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> result = null;
		try {
			hcpDTO = JSONObject.toJavaObject(transferJson,
					HealthCareProviderDTO.class);
			String providerid = (String) transferJson.remove("providerid");
			JSONObject temp = new JSONObject();
			temp.put("providerid", providerid);
			hcpDTOtemp = JSONObject.toJavaObject(temp,
					HealthCareProviderDTO.class);
			result = (Map<String, Object>) commonDAO.get(
					"healthcare.queryProvider", hcpDTOtemp);
			String isvalid = (String) result.get("isvalid");
			if (HealthCareConstants.NOT_VALID.equals(isvalid)) {
				resultMap
						.put(Constants.STATE_KEY, Constants.FAILED_STATE_VALUE);
				resultMap.put(Constants.MSG_KEY, "信息修改失败,该医院信息已被其他工作人员删除，请核实。");
				return resultMap;
			}
			commonDAO.update("healthcare.updateProvider", hcpDTO);
			resultMap.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			resultMap.put(Constants.MSG_KEY, Constants.UPDATE_SUCCESS);

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
			e.printStackTrace();
		}
		return resultMap;
	}

	public void setCommonDAO(CommonDAO dao) {
		this.commonDAO = dao;
	}

	@Transactional
	@Override
	public Map<String, Object> deleteProvider(JSONObject transferJson)
			throws BusinessServiceException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> parms = new HashMap<String, Object>();
		try {
			String ids = transferJson.getString("ids");
			StringBuffer sb = new StringBuffer();
			String[] id = ids.split(",");
			for (int i = 0; i < id.length; i++) {
				sb.append("'" + id[i] + "',");
			}
			ids = sb.toString().substring(0, sb.toString().lastIndexOf(','));
			parms.put("providerids", ids);
			parms.put("updateby", transferJson.getString("updateby"));
			commonDAO.update("healthcare.deleteProvider", parms);
			commonDAO.update("healthcare.deleteMedicalFacility", parms);
			commonDAO.update("healthcare.deleteBreach", parms);
			commonDAO.update("healthcare.deleteAccount", parms);
			commonDAO.update("healthcare.deleteContact", parms);
			commonDAO.update("healthcare.deleteDiagnosis", parms);
			commonDAO.update("healthcare.deleteDoctor", parms);
			resultMap.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
			resultMap.put(Constants.MSG_KEY, Constants.DELETE_SUCCESS);

		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
			e.printStackTrace();
		}
		return resultMap;
	}
}
